package com.shenchuangit.webprintclient.server;

import com.shenchuangit.webprintclient.cycle.AbstractPrintJobHandler;
import com.shenchuangit.webprintclient.cycle.AbstractPrintJobPreHandler;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class PrintJobServlet extends HttpServlet {

    private AbstractPrintJobPreHandler printJobPreHandler;
    private AbstractPrintJobHandler printJobHandler;

    public PrintJobServlet() {
    }

    @Override
    public void init() {
        // 执行必需的初始化
    }

    @Override
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws IOException {
        // 设置响应内容类型
        response.setContentType("text/html");

        System.out.println("打印前预处理");
        printJobPreHandler.prePrint(request);

        System.out.println("打印业务开始");
        printJobHandler.print(printJobPreHandler.getFileToPrint());

        // 实际的逻辑是在这里
        PrintWriter out = response.getWriter();
        out.println("success");
    }

    @Override
    public void destroy() {
        // 什么也不做
    }

    public void setPrintJobPreHandler(AbstractPrintJobPreHandler printJobPreHandler) {
        this.printJobPreHandler = printJobPreHandler;
    }

    public void setPrintJobHandler(AbstractPrintJobHandler printJobHandler) {
        this.printJobHandler = printJobHandler;
    }
}
